package com.snow.auth.controller;

import com.snow.annotation.SysLog;
import com.snow.auth.component.AuthComponent;
import com.snow.auth.entity.SystemRole;
import com.snow.auth.service.SystemRoleService;
import com.snow.conditions.ConditionType;
import com.snow.conditions.QueryConditionField;
import com.snow.jpa.LambdaQueryBuilder;
import com.snow.jpa.model.BasePageParams;
import com.snow.model.ILoginUser;
import com.snow.model.IdParams;
import com.snow.view.IWebResponse;
import com.snow.view.PageData;
import com.snow.view.WebResponse;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色管理
 *
 * @author by 13119
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system-role")
public class SystemRoleController {
    SystemRoleService systemRoleService;

    AuthComponent authComponent;

    @SysLog("创建角色")
    @PostMapping("/create")
    public WebResponse<SystemRole> create(@Validated @RequestBody SystemRole params) {
        return WebResponse.success(systemRoleService.save(params));
    }

    @SysLog("删除角色")
    @PostMapping("/delete")
    public WebResponse<SystemRole> delete(@RequestBody IdParams params) {
        if (params.getId() == 1) {
            return WebResponse.fail("内置角色禁止删除");
        }
        return WebResponse.success(systemRoleService.delete(params.getId()));
    }

    @SysLog("更新角色")
    @PostMapping("/update")
    public IWebResponse update(@Validated @RequestBody SystemRole role) {
        return WebResponse.success(systemRoleService.update(role));
    }

    @SysLog("获取所有角色")
    @PostMapping("/get-all")
    public IWebResponse getAll() {
        return WebResponse.success(systemRoleService.getRepository().findAll());
    }

    @Getter
    @Setter
    public static class PageParams extends BasePageParams {
        /**
         * 角色编码
         */
        @QueryConditionField(type = ConditionType.like)
        private String code;

        /**
         * 角色名称
         */
        @QueryConditionField(type = ConditionType.like)
        private String name;

        /**
         * 租户
         */
        @QueryConditionField
        private String tenantId;
    }

    @SysLog("分页查询角色列表")
    @PostMapping("/page")
    public WebResponse<PageData<SystemRole>> page(@RequestBody PageParams params) {
        ILoginUser loginUser = authComponent.getCurrentUser();
        if (loginUser.isSystemAdmin()) {
            LambdaQueryBuilder.disableTenantFilter();
        }
        return WebResponse.success(systemRoleService.page(params));
    }


    @PostMapping("/search")
    public WebResponse<List<SystemRole>> search() {
        return WebResponse.success(systemRoleService.search(null));
    }

}
